<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改家族婚配管理')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-marry-edit" th:object="${fMarry}">
            <input name="mId" th:field="*{mId}" type="hidden">
            <input id="memberId_hidden" th:value="*{memberId}" type="hidden">
            <input id="mateId_hidden" th:value="*{mateId}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required"> 所属家族：</label>
                <div class="col-sm-8">
                    <select class="form-control" id="surnameId" name="surnameId" disabled="disabled" onchange="surnameIdChangeFun()" required>
                        <option value="">请选择</option>
                        <option th:each="dict : ${fcanList}" th:text="${dict.name}" th:value="${dict.cId}" th:selected="${fMarry.surnameId == dict.cId ? true : false}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">家族成员：</label>
                <div class="col-sm-8">
                    <select id="memberId" name="memberId" class="form-control select2-multiple" required>
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">配偶成员：</label>
                <div class="col-sm-8">
                    <select id="mateId" name="mateId" class="form-control select2-multiple" required>
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" th:field="*{remark}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "family/marry";
        $("#form-marry-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-marry-edit').serialize());
            }
        }

        var isFirst = true;//是否首次执行
        //家族下拉列表改变触发
        function surnameIdChangeFun()
        {
            var surnameIdVal = $("#surnameId").val();
            console.log("列表改变的值：" + surnameIdVal);
            if(surnameIdVal != '')
            {
                //家族改变不管是否请求是否成功都得清空家族成员列表和配偶成员列表的值
                var memberId_hidden_val = null;
                var mateId_hidden_val = null;
                if(isFirst){
                    //获取默认的家族成员编号
                    memberId_hidden_val = $("#memberId_hidden").val();
                    //获取默认的配偶编号
                    mateId_hidden_val = $("#mateId_hidden").val();
                }
                //父亲下拉列表
                var memberIdSEL = $("#memberId");
                memberIdSEL.empty();
                memberIdSEL.append("<option value=''>请选择</option>");
                //母亲下拉列表
                var mateIdSEL = $("#mateId");
                mateIdSEL.empty();
                mateIdSEL.append("<option value=''>请选择</option>");
                $.ajax({
                    url: prefix + "/clanSelectChange",
                    type: 'GET',
                    data: {
                        surnameId: surnameIdVal,
                        memberId: memberId_hidden_val,
                        mateId: mateId_hidden_val
                    },
                    success: function (data) {
                        if(data.code == 0 && data.rows.length > 0){
                            //联动家族成员下拉列表
                            var memberList = data.rows[0].memberList;
                            if(memberList.length > 0){
                                for (var k = 0; k < memberList.length; k++) {
                                    var memSelectedStr = "";
                                    if(isFirst){
                                        if(memberId_hidden_val == memberList[k].mId){
                                            memSelectedStr = " selected ";
                                        }
                                    }
                                    memberIdSEL.append("<option value='" + memberList[k].mId + "'" + memSelectedStr + ">" + memberList[k].fullName + "（编号：" + memberList[k].mId + "）</option>");
                                }
                            }
                            //联动配偶成员下拉列表
                            var fMateList = data.rows[0].fMateList;
                            if(fMateList.length > 0){
                                for (var j = 0; j < fMateList.length; j++) {
                                    var mateSelectedStr = "";
                                    if(isFirst){
                                        if(mateId_hidden_val == fMateList[j].mId){
                                            mateSelectedStr = " selected ";
                                        }
                                    }
                                    mateIdSEL.append("<option value='" + fMateList[j].mId + "'" + mateSelectedStr + ">" + fMateList[j].fullName + "（编号：" + fMateList[j].mId + "）</option>");
                                }
                            }
                            //执行完毕设置为非首次执行
                            isFirst = false;
                        }
                    }
                });
            }
        }

        //家族成员下拉列表选项改变
        $("#memberId").change(function (){
            var memberIdVal = $("#memberId").val();
            console.log("家族成员下拉列表的值：" + memberIdVal);
            if(memberIdVal != ''){
                var surnameIdVal = $("#surnameId").val();
                var mateIdVal = $("#mateId").val();
                //清空配偶成员下拉列表
                var mateIdSEL = $("#mateId");
                mateIdSEL.empty();
                mateIdSEL.append("<option value=''>请选择</option>");
                $.ajax({
                    url: prefix + "/memberSelectChange",
                    type: 'GET',
                    data: {
                        surnameId: surnameIdVal,
                        memberId: memberIdVal,
                        mateId: mateIdVal
                    },
                    success: function (data) {
                        if(data.code == 0 && data.rows.length > 0){
                            var fMateList = data.rows;
                            for (var i = 0; i < fMateList.length; i++) {
                                var mateSelectedStr = "";
                                if(mateIdVal == fMateList[i].mId){
                                    mateSelectedStr = " selected ";
                                }
                                mateIdSEL.append("<option value='" + fMateList[i].mId + "'" + mateSelectedStr + ">" + fMateList[i].fullName + "（编号：" + fMateList[i].mId + "）</option>");
                            }
                        }
                    }
                });
            }
        });

        //配偶成员下拉列表选项改变
        $("#mateId").change(function (){
            var mateIdVal = $("#mateId").val();
            console.log("配偶成员下拉列表的值：" + mateIdVal == '');
            if(mateIdVal != '') {
                var surnameIdVal = $("#surnameId").val();
                var memberIdVal = $("#memberId").val();
                //清空家族成员下拉列表
                var memberIdSEL = $("#memberId");
                memberIdSEL.empty();
                memberIdSEL.append("<option value=''>请选择</option>");
                $.ajax({
                    url: prefix + "/mateSelectChange",
                    type: 'GET',
                    data: {
                        surnameId: surnameIdVal,
                        memberId: memberIdVal,
                        mateId: mateIdVal
                    },
                    success: function (data) {
                        if (data.code == 0 && data.rows.length > 0) {
                            var memberList = data.rows;
                            for (var i = 0; i < memberList.length; i++) {
                                var memberSelectedStr = "";
                                if(memberIdVal == memberList[i].mId){
                                    memberSelectedStr = " selected ";
                                }
                                memberIdSEL.append("<option value='" + memberList[i].mId + "'" + memberSelectedStr + ">" + memberList[i].fullName + "（编号：" + memberList[i].mId + "）</option>");
                            }
                        }
                    }
                });
            }
        });

        $(function() {
            //设置默认的家族成员、配偶成员等下拉列表
            surnameIdChangeFun();
        });
    </script>
</body>
</html>